<template>
  <el-card class="metadata-card">
    <template #header>
      <div class="card-header">
        <span>题目类型管理</span>
        <el-button type="primary" @click="handleAddType">添加类型</el-button>
      </div>
    </template>

    <el-table :data="questionTypes" border style="width: 100%">
      <el-table-column prop="name" label="类型名称" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="handleEditType(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDeleteType(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
      <el-form :model="typeForm" label-width="80px">
        <el-form-item label="类型名称">
          <el-input v-model="typeForm.name" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitTypeForm">确认</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import questionTypesService from '../../services/questionTypes'

const questionTypes = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const typeForm = ref({
  id: '',
  name: ''
})
const isEditMode = ref(false)

const fetchQuestionTypes = async () => {
  try {
    const fetquestionTypes = await questionTypesService.fetchQuestionTypes()
    questionTypes.value = fetquestionTypes.data
    /**
     *  或者把上面两句改成
     *  const { data } = await questionTypesService.fetchQuestionTypes(); // 解构赋值
     *  questionTypes.value = data;
     * 
     *  绝不能
     *  const fetquestionTypes = await questionTypesService.fetchQuestionTypes().data
     *  questionTypes.value = fetquestionTypes
     */
  } catch (error) {
    ElMessage.error(error.message)
  }
}

onMounted(() => {
  fetchQuestionTypes()
})

const handleAddType = () => {
  dialogTitle.value = '添加题目类型'
  typeForm.value = { id: '', name: '' }
  isEditMode.value = false
  dialogVisible.value = true
}

const handleEditType = (row) => {
  dialogTitle.value = '编辑题目类型'
  typeForm.value = { ...row }
  isEditMode.value = true
  dialogVisible.value = true
}

const handleDeleteType = async (row) => {
  try {
    await ElMessageBox.confirm('确定删除该题目类型吗?一旦删除，原有的题目类型就算被重建也和以前不一样了', '提示', { 
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await questionTypesService.deleteQuestionType(row.id)
    ElMessage.success('已删除该题目类型')
    fetchQuestionTypes()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error(error.message)
    }
  }
}

const submitTypeForm = async () => {
  try {
    if (isEditMode.value) {
      await questionTypesService.updateQuestionType(typeForm.value.id, { 
        name: typeForm.value.name
      })
      ElMessage.success('更新成功')
    } else { // 这里面大部分逻辑没有问题，但是关于“已重新启用题目类型”与以前类型的删除用禁用代替有关，现在model中不存在
      const result = await questionTypesService.createQuestionType({ 
        name: typeForm.value.name
      })
      ElMessage.success(result.reactivated ? '已重新启用题目类型' : '添加成功')
    }
    dialogVisible.value = false
    fetchQuestionTypes()
  } catch (error) {
    ElMessage.error(`${error.message}`)
  }
}

defineExpose({
  fetchQuestionTypes
})
</script>

<style scoped>
.metadata-card {
  margin-top: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  border: 1px solid #eee;
}

.metadata-card :deep(.el-card__header) {
  background: #f8f9fa;
  border-bottom: 1px solid #eee;
  padding: 15px 20px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.card-header span {
  font-size: 1.1rem;
  font-weight: 600;
  color: #333;
}

:deep(.el-table) {
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.05);
}

:deep(.el-table th) {
  background: #f8f9fa;
  font-weight: 600;
}

:deep(.el-table tr:hover) {
  background: #f0f7ff;
}

:deep(.el-table td) {
  padding: 10px 0;
}

:deep(.el-dialog) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-dialog__header) {
  background: #f5f7fa;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}

:deep(.el-dialog__title) {
  font-size: 1.1rem;
  font-weight: 600;
  color: #333;
}

:deep(.el-dialog__body) {
  padding: 20px;
}

:deep(.el-dialog__footer) {
  padding: 12px 20px;
  background: #f5f7fa;
  border-top: 1px solid #eee;
}

:deep(.el-form-item) {
  margin-bottom: 15px;
}

:deep(.el-input) {
  border-radius: 4px;
}

:deep(.el-button) {
  border-radius: 4px;
  transition: all 0.3s ease;
  font-size: 0.9rem;
}

:deep(.el-button--primary) {
  box-shadow: 0 1px 4px rgba(64, 158, 255, 0.3);
}

:deep(.el-button--primary:hover) {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.4);
}

:deep(.el-button--danger) {
  box-shadow: 0 1px 4px rgba(245, 108, 108, 0.3);
}

:deep(.el-button--danger:hover) {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(245, 108, 108, 0.4);
}

@media (max-width: 768px) {
  .card-header {
    flex-direction: column;
    align-items: stretch;
  }
  
  :deep(.el-table) {
    font-size: 0.85rem;
  }
  
  .metadata-card {
    margin-top: 12px;
  }
}
</style>
